package com.jinghua.treatment.common.controller;

import com.jinghua.treatment.common.Pager;
import com.jinghua.treatment.common.domain.JSONResult;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpHeaders;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


/**
 * Created by kriss on 2018/4/26.
 */
public class BaseController {

    protected static final Logger logger = LoggerFactory.getLogger(BaseController.class);
    protected HttpServletRequest request;
    protected HttpServletResponse response;
    protected HttpSession session;
    protected HttpHeaders headers;
    protected Integer page;
    protected Pager pager;


    @ModelAttribute
    protected void initRequestResponseSessionHeaders(HttpServletRequest request,
                                                     HttpServletResponse response,
                                                     @RequestHeader HttpHeaders headers,
                                                     RedirectAttributes redirectAttributes) {
        this.request = request;
        this.response = response;
        this.session = request.getSession();
        this.headers = headers;

        String pageStr = request.getParameter("page");

        pager = new Pager();
        if (StringUtils.isNotEmpty(pageStr)) {
            logger.debug("####页码参数page:" + pageStr);
            page = Integer.parseInt(pageStr);
            pager.setPage(page);
        }
    }
    protected JSONResult jsonResult(String code, String msg, Object data) {
        return new JSONResult(code, msg, data);
    }

}
